package com.api.gbhg.daiyanlu.statement;

import com.alibaba.fastjson.JSON;
import com.api.gbhg.daiyanlu.Dao.TheReportCenterDao;
import com.api.gbhg.daiyanlu.DataPrintCopyUtil.PageInfo;
import com.api.gbhg.liuwenbo.utils.AlgorithmUtil;
import weaver.conn.RecordSet;

import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.Map;

/**
 * 经济支出分类统计报表
 * Mr.dyl
 */
@Path("/classified")
public class ClassifiedStatistics {
    TheReportCenterDao dao = new TheReportCenterDao();
    BigDecimal bd7 = new BigDecimal("100");
    private PageInfo[] PageInfo;
    private String bm;
    private String dw;
    @POST
    @Path("/getClassified")
    @Produces(MediaType.APPLICATION_JSON)
    public String getData(Map<String, Object> param) {
        this.init(param);
        this.government();//政府经济支出统计
        this.department();//部门经济支出分类
        return JSON.toJSONString(this.PageInfo);
    }


    private void init(Map<String, Object> param) {
        this.PageInfo = new PageInfo[2];
        this.setBm(param);
        this.setDw(param);
    }

    public void setDw(Map<String, Object> param){
        if(param.containsKey("dw") && param.get("dw") instanceof Integer ){
            this.dw = AlgorithmUtil.getSubCompanIdBybmId((Integer) param.get("dw"));
        }else{
            this.dw = "2"; //默认为2(拱北海关)
        }
    }

    public void setBm(Map<String, Object> param){
        if(param.containsKey("bm") && param.get("bm") instanceof Integer ){
            this.bm = TheReportCenterDao.getSectionId((Integer) param.get("bm"));
        }else{
            this.bm = "1";//1为查询所有部门
        }
    }
    /**
     * 政府经济支出统计
     */
    public void government(){
        //政府经济支出统计报表总金额
        RecordSet rs=dao.getClassifiedSum(dw,bm);
        BigDecimal zje=new BigDecimal("0.00");
        while (rs.next()){
            zje=new BigDecimal(rs.getString("ze"));
        }
        //[501]机关工资福利支出
        rs = dao.getWageWelfare(dw,bm);
        BigDecimal je1=new BigDecimal("0.00");
        BigDecimal bfb1=new BigDecimal("0.00");
        while (rs.next()) {
            je1=new BigDecimal(rs.getString("je1"));
            bfb1=je1.divide(zje,2, RoundingMode.HALF_UP).multiply(bd7);
        }
        //[502]机关商品和服务支出
        rs = dao.GoodsServices(dw,bm);
        BigDecimal je2=new BigDecimal("0.00");
        BigDecimal bfb2=new BigDecimal("0.00");
        while (rs.next()) {
            je2=new BigDecimal(rs.getString("je2"));
            bfb2=je2.divide(zje,2, RoundingMode.HALF_UP).multiply(bd7);
        }
        //[503]机关资本性支出（一）
        rs = dao.capitalizationOne(dw,bm);
        BigDecimal je3=new BigDecimal("0.00");
        BigDecimal bfb3=new BigDecimal("0.00");
        while (rs.next()) {
            je3=new BigDecimal(rs.getString("je3"));
            bfb3=je3.divide(zje,2, RoundingMode.HALF_UP).multiply(bd7);
        }
        //[504]机关资本性支出（二）
        rs = dao.capitalizationTwo(dw,bm);
        BigDecimal je4=new BigDecimal("0.00");
        BigDecimal bfb4=new BigDecimal("0.00");
        while (rs.next()) {
            je4=new BigDecimal(rs.getString("je4"));
            bfb4=je4.divide(zje,2, RoundingMode.HALF_UP).multiply(bd7);
        }
        //[505]对事业单位经常性补助
        rs = dao.PublicOften(dw,bm);
        BigDecimal je5=new BigDecimal("0.00");
        BigDecimal bfb5=new BigDecimal("0.00");
        while (rs.next()) {
            je5=new BigDecimal(rs.getString("je5"));
            bfb5=je5.divide(zje,2, RoundingMode.HALF_UP).multiply(bd7);
        }
        //[506]对事业单位资本性补助
        rs = dao.PublicCapital(dw,bm);
        BigDecimal je6=new BigDecimal("0.00");
        BigDecimal bfb6=new BigDecimal("0.00");
        while (rs.next()) {
            je6=new BigDecimal(rs.getString("je6"));
            bfb6=je6.divide(zje,2, RoundingMode.HALF_UP).multiply(bd7);
        }
        //[507]对企业补助
        rs = dao.SubsidiesToEnterprises(dw,bm);
        BigDecimal je7=new BigDecimal("0.00");
        BigDecimal bfb7=new BigDecimal("0.00");
        while (rs.next()) {
            je7=new BigDecimal(rs.getString("je7"));
            bfb7=je7.divide(zje,2, RoundingMode.HALF_UP).multiply(bd7);
        }
        //[508]对企业资本性支出
        rs = dao.apitalToBusiness(dw,bm);
        BigDecimal je8=new BigDecimal("0.00");
        BigDecimal bfb8=new BigDecimal("0.00");
        while (rs.next()) {
            je8=new BigDecimal(rs.getString("je8"));
            bfb8=je8.divide(zje,2, RoundingMode.HALF_UP).multiply(bd7);
        }
        //[509]对个人和家庭的补助
        rs = dao.individualsFamilies(dw,bm);
        BigDecimal je9=new BigDecimal("0.00");
        BigDecimal bfb9=new BigDecimal("0.00");
        while (rs.next()) {
            je9=new BigDecimal(rs.getString("je9"));
            bfb9=je9.divide(zje,2, RoundingMode.HALF_UP).multiply(bd7);
        }
        //[510]对社会保障基金补助
        rs = dao.socialInsurance(dw,bm);
        BigDecimal je10=new BigDecimal("0.00");
        BigDecimal bfb10=new BigDecimal("0.00");
        while (rs.next()) {
            je10=new BigDecimal(rs.getString("je10"));
            bfb10=je10.divide(zje,2, RoundingMode.HALF_UP).multiply(bd7);
        }
        //[511]债务利息及费用支出
        rs = dao.debtInteres(dw,bm);
        BigDecimal je11=new BigDecimal("0.00");
        BigDecimal bfb11=new BigDecimal("0.00");
        while (rs.next()) {
            je11=new BigDecimal(rs.getString("je11"));
            bfb11=je11.divide(zje,2, RoundingMode.HALF_UP).multiply(bd7);
        }
        //[512]债务还本支出
        rs = dao.repaymentPrincipal(dw,bm);
        BigDecimal je12=new BigDecimal("0.00");
        BigDecimal bfb12=new BigDecimal("0.00");
        while (rs.next()) {
            je12=new BigDecimal(rs.getString("je12"));
            bfb12=je12.divide(zje,2, RoundingMode.HALF_UP).multiply(bd7);
        }
        //[513]转移性支出
        rs = dao.transfer(dw,bm);
        BigDecimal je13=new BigDecimal("0.00");
        BigDecimal bfb13=new BigDecimal("0.00");
        while (rs.next()) {
            je13=new BigDecimal(rs.getString("je13"));
            bfb13=je13.divide(zje,2, RoundingMode.HALF_UP).multiply(bd7);
        }
        //[514]预备费及预留
        rs = dao.ReservesReservations(dw,bm);
        BigDecimal je14=new BigDecimal("0.00");
        BigDecimal bfb14=new BigDecimal("0.00");
        while (rs.next()) {
            je14=new BigDecimal(rs.getString("je14"));
            bfb2=je14.divide(zje,2, RoundingMode.HALF_UP).multiply(bd7);
        }
        //[599]其他支出
        rs = dao.otherExpenses(dw,bm);
        BigDecimal je15=new BigDecimal("0.00");
        BigDecimal bfb15=new BigDecimal("0.00");
        while (rs.next()) {
            je15=new BigDecimal(rs.getString("je15"));
            bfb15=je15.divide(zje,2, RoundingMode.HALF_UP).multiply(bd7);
        }
        Map<String, Object>[] map = new HashMap[15];
        this.PageInfo[0] = new PageInfo();
        //[501]机关工资福利支出
        map[0] = new HashMap();
        map[0].put("lmc", "机关工资福利支出");
        map[0].put("je", je1);
        map[0].put("bfb", bfb1+"%");
        //[502]机关商品和服务支出
        map[1] = new HashMap();
        map[1].put("lmc", "机关商品和服务支出");
        map[1].put("je", je2);
        map[1].put("bfb", bfb2+"%");
        //[503]机关资本性支出（一）
        map[2] = new HashMap();
        map[2].put("lmc", "机关资本性支出（一）");
        map[2].put("je", je3);
        map[2].put("bfb", bfb3+"%");
        //[504]机关资本性支出（二）
        map[3] = new HashMap();
        map[3].put("lmc", "机关资本性支出（二）");
        map[3].put("je", je4);
        map[3].put("bfb", bfb4+"%");
        //[505]对事业单位经常性补助
        map[4] = new HashMap();
        map[4].put("lmc", "对事业单位经常性补助");
        map[4].put("je", je5);
        map[4].put("bfb", bfb5+"%");
        //[506]对事业单位资本性补助
        map[5] = new HashMap();
        map[5].put("lmc","对事业单位资本性补助");
        map[5].put("je", je6);
        map[5].put("bfb", bfb6+"%");
        //[507]对企业补助
        map[6] = new HashMap();
        map[6].put("lmc", "对企业补助");
        map[6].put("je", je7);
        map[6].put("bfb", bfb7+"%");
        //[508]对企业资本性支出
        map[7] = new HashMap();
        map[7].put("lmc", "对企业资本性支出");
        map[7].put("je", je8);
        map[7].put("bfb", bfb8+"%");
        //[509]对个人和家庭的补助
        map[8] = new HashMap();
        map[8].put("lmc", "对个人和家庭的补助");
        map[8].put("je", je9);
        map[8].put("bfb", bfb9+"%");
        //[510]对社会保障基金补助
        map[9] = new HashMap();
        map[9].put("lmc", "对社会保障基金补助");
        map[9].put("je", je10);
        map[9].put("bfb", bfb10+"%");
        //[511]债务利息及费用支出
        map[10] = new HashMap();
        map[10].put("lmc", "债务利息及费用支出");
        map[10].put("je", je11);
        map[10].put("bfb", bfb11+"%");
        //[512]债务还本支出
        map[11] = new HashMap();
        map[11].put("lmc", "债务还本支出");
        map[11].put("je", je12);
        map[11].put("bfb", bfb12+"%");
        //[513]转移性支出
        map[12] = new HashMap();
        map[12].put("lmc", "转移性支出");
        map[12].put("je", je13);
        map[12].put("bfb", bfb13+"%");
        //[514]预备费及预留
        map[13] = new HashMap();
        map[13].put("lmc", "预备费及预留");
        map[13].put("je", je14);
        map[13].put("bfb", bfb14+"%");
        //[599]其他支出
        map[14] = new HashMap();
        map[14].put("lmc", "其他支出");
        map[14].put("je", je15);
        map[14].put("bfb", bfb15+"%");
        this.PageInfo[0].setTitle("政府经济支出统计");
        this.PageInfo[0].setZje(zje);
        this.PageInfo[0].setData(map);
    }

    /**
     * 部门经济支出分类
     */
    public void department(){
        //部门经济支出分类总金额
        RecordSet rs=dao.getSectoralSum(dw,bm);
        BigDecimal zje=new BigDecimal("0.00");
        while (rs.next()){
            zje=new BigDecimal(rs.getString("ze"));
        }
        //[301]工资福利支出
        rs = dao.getClassifiedDepartment(dw,bm);
        BigDecimal je1=new BigDecimal("0.00");
        BigDecimal bfb1=new BigDecimal("0.00");
        while (rs.next()) {
            je1=new BigDecimal(rs.getString("je1"));
            bfb1=je1.divide(zje,2, RoundingMode.HALF_UP).multiply(bd7);
        }
        //[302]商品和服务支出
        rs = dao.GoodsAndServices(dw,bm);
        BigDecimal je2=new BigDecimal("0.00");
        BigDecimal bfb2=new BigDecimal("0.00");
        while (rs.next()) {
            je2=new BigDecimal(rs.getString("je2"));
            bfb2=je2.divide(zje,2, RoundingMode.HALF_UP).multiply(bd7);
        }
        //[303]对个人和家庭的补助
        rs = dao.SubsidiesIndividuals(dw,bm);
        BigDecimal je3=new BigDecimal("0.00");
        BigDecimal bfb3=new BigDecimal("0.00");
        while (rs.next()) {
            je3=new BigDecimal(rs.getString("je3"));
            bfb3=je3.divide(zje,2, RoundingMode.HALF_UP).multiply(bd7);
        }
        //[307]债务利息及费用支出
        rs = dao.SubsidizedDebt(dw,bm);
        BigDecimal je7=new BigDecimal("0.00");
        BigDecimal bfb7=new BigDecimal("0.00");
        while (rs.next()) {
            je7=new BigDecimal(rs.getString("je7"));
            bfb7=je7.divide(zje,2, RoundingMode.HALF_UP).multiply(bd7);
        }
        //[309]资本性支出（基本建设）
        rs = dao.CapitalExpenditure(dw,bm);
        BigDecimal je9=new BigDecimal("0.00");
        BigDecimal bfb9=new BigDecimal("0.00");
        while (rs.next()) {
            je9=new BigDecimal(rs.getString("je9"));
            bfb9=je9.divide(zje,2, RoundingMode.HALF_UP).multiply(bd7);
        }
        //[310]资本性支出
        rs = dao.CapitalExpenditures(dw,bm);
        BigDecimal je10=new BigDecimal("0.00");
        BigDecimal bfb10=new BigDecimal("0.00");
        while (rs.next()) {
            je10=new BigDecimal(rs.getString("je10"));
            bfb10=je10.divide(zje,2, RoundingMode.HALF_UP).multiply(bd7);
        }
        //[311]对企业补助（基本建设）
        rs = dao.enterprises(dw,bm);
        BigDecimal je11=new BigDecimal("0.00");
        BigDecimal bfb11=new BigDecimal("0.00");
        while (rs.next()) {
            je11=new BigDecimal(rs.getString("je11"));
            bfb11=je11.divide(zje,2, RoundingMode.HALF_UP).multiply(bd7);
        }
        //[312]对企业补助
        rs = dao.toEnterprises(dw,bm);
        BigDecimal je12=new BigDecimal("0.00");
        BigDecimal bfb12=new BigDecimal("0.00");
        while (rs.next()) {
            je12=new BigDecimal(rs.getString("je12"));
            bfb12=je12.divide(zje,2, RoundingMode.HALF_UP).multiply(bd7);
        }
        //[313]对社会保障基金补助
        rs = dao.socialSecurity(dw,bm);
        BigDecimal je13=new BigDecimal("0.00");
        BigDecimal bfb13=new BigDecimal("0.00");
        while (rs.next()) {
            je13=new BigDecimal(rs.getString("je13"));
            bfb13=je13.divide(zje,2, RoundingMode.HALF_UP).multiply(bd7);
        }
        //[399]其他支出
        rs = dao.expenses(dw,bm);
        BigDecimal je99=new BigDecimal("0.00");
        BigDecimal bfb99=new BigDecimal("0.00");
        while (rs.next()) {
            je99=new BigDecimal(rs.getString("je99"));
            bfb99=je99.divide(zje,2, RoundingMode.HALF_UP).multiply(bd7);
        }
        Map<String, Object>[] map = new HashMap[10];
        this.PageInfo[1] = new PageInfo();
        //[301]工资福利支出
        map[0] = new HashMap();
        map[0].put("lmc", "工资福利支出");
        map[0].put("je", je1);
        map[0].put("bfb", bfb1+"%");
        //[302]商品和服务支出
        map[1] = new HashMap();
        map[1].put("lmc", "商品和服务支出");
        map[1].put("je", je2);
        map[1].put("bfb", bfb2+"%");
        //[303]对个人和家庭的补助
        map[2] = new HashMap();
        map[2].put("lmc", "对个人和家庭的补助");
        map[2].put("je", je3);
        map[2].put("bfb", bfb3+"%");
        //[307]债务利息及费用支出
        map[3] = new HashMap();
        map[3].put("lmc", "债务利息及费用支出");
        map[3].put("je", je7);
        map[3].put("bfb", bfb7+"%");
        //[309]资本性支出（基本建设）
        map[4] = new HashMap();
        map[4].put("lmc", "资本性支出（基本建设）");
        map[4].put("je", je9);
        map[4].put("bfb", bfb9+"%");
        //[310]资本性支出
        map[5] = new HashMap();
        map[5].put("lmc","资本性支出");
        map[5].put("je", je10);
        map[5].put("bfb", bfb10+"%");
        //[311]对企业补助（基本建设）
        map[6] = new HashMap();
        map[6].put("lmc", "对企业补助（基本建设）");
        map[6].put("je", je11);
        map[6].put("bfb", bfb11+"%");
        //[312]对企业补助
        map[7] = new HashMap();
        map[7].put("lmc", "对企业补助");
        map[7].put("je", je12);
        map[7].put("bfb", bfb12+"%");
        //[313]对社会保障基金补助
        map[8] = new HashMap();
        map[8].put("lmc", "对社会保障基金补助");
        map[8].put("je", je13);
        map[8].put("bfb", bfb13+"%");
        //[399]其他支出
        map[9] = new HashMap();
        map[9].put("lmc", "其他支出");
        map[9].put("je", je99);
        map[9].put("bfb", bfb99+"%");
        this.PageInfo[1].setTitle("部门经济支出分类");
        this.PageInfo[1].setZje(zje);
        this.PageInfo[1].setData(map);
    }
}
